package com.tanhua.server.service;

import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.UserLocation;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.dubbo.api.mongo.UserLocationApi;
import com.tanhua.server.interceptors.UserHolder;
import com.tanhua.vo.NearUserVo;
import com.tanhua.vo.UserLocationVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.time.Year;
import java.util.ArrayList;
import java.util.List;

@Service
public class BaiduService {

    @Reference
    private UserLocationApi userLocationApi;

    @Reference
    private UserInfoApi userInfoApi;

    public ResponseEntity saveLocation(Double longitude, Double latitude, String addrStr) {

        userLocationApi.save(UserHolder.getUserId(),longitude,latitude,addrStr);



      return   ResponseEntity.ok(null);
    }

    public ResponseEntity search(String gender, Integer distance) {
        Long userId = UserHolder.getUserId();
        List<UserLocationVo> userLocationList = userLocationApi.search(userId,distance);

     List<NearUserVo> nearUserVoList = new ArrayList<>();

        for (UserLocationVo userLocationVo : userLocationList) {
            UserInfo userInfo = userInfoApi.findById(userLocationVo.getUserId());
//            查询的时候有一个性别 处理一下性别
//            1、自己要排除  2、符合性别
            if((userInfo.getId().longValue()==userId.longValue()) || !userInfo.getGender().equals(gender) ){ //先排除自己
                continue;
            }
//            userInfo---->NearUserVo
            NearUserVo nearUserVo = new NearUserVo();
            BeanUtils.copyProperties(userInfo,nearUserVo);
            nearUserVoList.add(nearUserVo);
        }
     return ResponseEntity.ok(nearUserVoList);

    }

}
